package com.peanut.baby.mvp.trend.list;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.Ad;
import com.peanut.baby.model.BBSMoment;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/23.
 */

public class TrendListPresenter implements TrendListContract.Presenter{
    private TrendListContract.View view;
    private Activity             activity;
    public TrendListPresenter(TrendListContract.View view, Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getUserMoments(String userId,int pageNo,int pageSize) {
        Observable obs = RetrofitClient.getInstance().getUserMoments(userId,
                pageNo,
                pageSize);
        obs.compose(BaseScheduler.<List<BBSMoment>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<BBSMoment>>() {
            @Override
            protected void onHandleSuccess(List<BBSMoment> o) {
                view.onMomentsGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetMomentsFailed(msg);
            }

        });
    }

    @Override
    public void getHotMoments(int pageNo, int pageSize) {
        Observable obs = RetrofitClient.getInstance().getHotMoments(2,pageNo,
                pageSize);
        obs.compose(BaseScheduler.<List<BBSMoment>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<BBSMoment>>() {
            @Override
            protected void onHandleSuccess(List<BBSMoment> o) {
                view.onMomentsGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetMomentsFailed(msg);
            }

        });
    }

    @Override
    public void getAllMoments(int pageNo, int pageSize) {
        Observable obs = RetrofitClient.getInstance().getTrendList(pageNo,
                pageSize);
        obs.compose(BaseScheduler.<List<BBSMoment>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<BBSMoment>>() {
                    @Override
                    protected void onHandleSuccess(List<BBSMoment> o) {
                        view.onMomentsGet(o);
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onGetMomentsFailed(msg);
                    }

                });
    }

    @Override
    public void getBanner(int type) {
        int param = 1;
        if(type == TrendListActivity.TYPE_SELF_CIRCLE){
            param = 5;
        }else if(type == TrendListActivity.TYPE_HOT_CIRCLE){
            param = 6;
        }
        Observable obs = RetrofitClient.getInstance().getBannerList(param);
        obs.compose(BaseScheduler.<BaseResponse<List<Ad>>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<Ad>>() {
                    @Override
                    protected void onHandleSuccess(List<Ad> ads) {
                        view.onBannerGet(true,ads,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onBannerGet(false,null,msg);
                    }

                });
    }

    @Override
    public void getCircleMoments(int pageNo, int pageSize) {
        //if not login
        if(InitManager.getInstance().getUser() == null){
            view.onGetMomentsFailed(null);
            return;
        }
        Observable obs = RetrofitClient.getInstance().getFollowingMoments(InitManager.getInstance().getUserId(),
                pageNo,
                pageSize,InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<List<BBSMoment>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<BBSMoment>>() {
            @Override
            protected void onHandleSuccess(List<BBSMoment> o) {
                view.onMomentsGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetMomentsFailed(msg);
            }

        });
    }
}
